/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package controller;

import java.io.IOException;
import java.io.PrintWriter;
import java.util.ArrayList;
import java.util.Date;
import java.util.HashSet;
import java.util.Set;
import javax.servlet.RequestDispatcher;
import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;
import javax.swing.JOptionPane;
import model.dao.*;
import model.pojo.*;

/**
 *
 * @author Suma
 */
@WebServlet(name = "CheckOut", urlPatterns = {"/CheckOut.do"})
public class CheckOut extends HttpServlet {

    /**
     * Processes requests for both HTTP
     * <code>GET</code> and
     * <code>POST</code> methods.
     *
     * @param request servlet request
     * @param response servlet response
     * @throws ServletException if a servlet-specific error occurs
     * @throws IOException if an I/O error occurs
     */
    protected void processRequest(HttpServletRequest request, HttpServletResponse response)
            throws ServletException, IOException {
        HttpSession session = request.getSession();
        double tongtien = 0;
        ArrayList<SanPham> gioHang;
        if (session.getAttribute("GioHang") == null) {
            gioHang = new ArrayList<SanPham>();
            session.setAttribute("GioHang", gioHang);
        } else {
            gioHang = (ArrayList<SanPham>) session.getAttribute("GioHang");
            for (int i = 0; i < gioHang.size(); i++) {
                SanPham s = gioHang.get(i);
                tongtien = tongtien + s.getDonGia() * s.getSoLuong();
            }
        }
        String action = request.getParameter("Action");
        if (action != null) {
            if (action.equals("ThanhToanLaKhach")) {
                String view = "index.jsp";
                request.setAttribute("Action", "ThanhToanLaKhach");
                request.setAttribute("page", "CheckOut");
                RequestDispatcher rd = request.getRequestDispatcher(view);
                rd.forward(request, response);
            } else {
                if (action.equals("ThanhToan")) {
                    GioHangDAO ghdao = new GioHangDAO();
                    String hoVaTen = "";
                    String soDienThoai = "";
                    String diaChi = "";
                    hoVaTen = request.getParameter("hoVaTen");
                    soDienThoai = request.getParameter("soDienThoai");
                    diaChi = request.getParameter("diaChi");
                    KhachHang kh;
                    kh = new KhachHang(KhachHangDAO.sinhMaKhachHang(), hoVaTen, "", soDienThoai, diaChi, "", 0, "", "", new Date(), new Date(), 0);
                    KhachHangDAO khdao = new KhachHangDAO();
                    khdao.saveOrUpdate(kh);
                    model.pojo.GioHang ddh = new model.pojo.GioHang();

                    ddh.setMaGioHang(GioHangDAO.sinhMaGioHang());
                    ddh.setNgayTao(new Date());
                    ddh.setKhachHang(kh);

                    ddh.setTongTien(tongtien);
                    ddh.setTinhTrang(0);
                    // JOptionPane.showMessageDialog(null, ddh.getMaDonDatHang());
                    for (int i = 0; i < gioHang.size(); i++) {
                        Set cthd = new HashSet(1);
                        SanPham s = gioHang.get(i);
                        ChiTietGioHangId ctid = new ChiTietGioHangId(ddh.getMaGioHang(), s.getMaSanPham());
                        model.pojo.ChiTietGioHang ct = new model.pojo.ChiTietGioHang(ctid, ddh, s, s.getDonGia(), s.getSoLuong());
                        cthd.add(ct);
                        ddh.setChiTietGioHangs(cthd);
                        ghdao.saveOrUpdate(ddh);

                    }
                    session.removeAttribute("GioHang");
                    JOptionPane.showMessageDialog(null, "Thanh toán xong");
                    String view = "TrangChu.do";
                    RequestDispatcher rd = request.getRequestDispatcher(view);
                    rd.forward(request, response);
                }
            }
        }

    }

    // <editor-fold defaultstate="collapsed" desc="HttpServlet methods. Click on the + sign on the left to edit the code.">
    /**
     * Handles the HTTP
     * <code>GET</code> method.
     *
     * @param request servlet request
     * @param response servlet response
     * @throws ServletException if a servlet-specific error occurs
     * @throws IOException if an I/O error occurs
     */
    @Override
    protected void doGet(HttpServletRequest request, HttpServletResponse response)
            throws ServletException, IOException {
        processRequest(request, response);
    }

    /**
     * Handles the HTTP
     * <code>POST</code> method.
     *
     * @param request servlet request
     * @param response servlet response
     * @throws ServletException if a servlet-specific error occurs
     * @throws IOException if an I/O error occurs
     */
    @Override
    protected void doPost(HttpServletRequest request, HttpServletResponse response)
            throws ServletException, IOException {
        processRequest(request, response);
    }

    /**
     * Returns a short description of the servlet.
     *
     * @return a String containing servlet description
     */
    @Override
    public String getServletInfo() {
        return "Short description";
    }// </editor-fold>
}
